package CtrLayer;
import ModelLayer.*;
/**
 * OrderCtr collect and control methods related to class Order, class OrderContainer and class ItemContainer. 
 * 
 * @author Gruppe 3 
 * @version 20-12-2011
 */
public class OrderCtr
{
    private ItemCtr itemCtr;
    private OrderContainer orderContainer;
    private ItemContainer itemContainer;
    private Order order;
    private static OrderCtr instance;

    
    /**
     * Constructs an object of class OrderCtr and intialize orderContainer and itemContainer. 
     */
    public OrderCtr()
    {
        orderContainer = orderContainer.getInstance();
        itemContainer = itemContainer.getInstance();
    }
    
    public static OrderCtr getInstance()
    {
        if(instance==null)
        {
            instance = new OrderCtr();
        }
        return instance;
    }

    
    
    /**
     * Downsize amount on the items on the order by the quantity on the partOrders and returns the total price of the whole order. 
     * 
     * @return totalPrice, the totalPrice of the order. 
     */

    public double endSale()
    {
        order.downsizeInventory();
        return order.getTotalPrice(); 
    }
    
    /**
     * Returns the change the customer needs back by typing in how much he paid.
     * 
     * @param amount, the amount of money the customer pays in kr.
     * @return change, the amount of change the customer shall receive in kr.
     */

    public double makePayment(double amount)
    {
        return amount - order.getTotalPrice();
        
    }
    
    /**
     * Creates a new order, adds it to the orderContainer and returns the new order object.
     * 
     * @param discount, order wide discount in %.
     * @param employee, the employee who handled the order.
     * @param customer, the customer who made the order. 
     * @return order, the order that was just created.
     */

    public Order createOrder(double discount, Person employee, Person customer)
    {
        order = new Order(discount, employee, customer);
        orderContainer.addOrder(order);
        return order;
    }
    
    /**
     * Returns an order object. 
     * 
     * @return order, the order associated with orderCtr.
     */

    public Order getOrder()
    {
        return order;
    }
    
    /**
     * Finds an item by searching on the items barCode.
     * 
     * @return item2, the item that matches the barCode searched on. 
     */
    
    public Item findItem(String barCode)
    {
        Item item2 = itemContainer.findItem(barCode);
        return item2;
    }

}
